package com.useeinfo.oa.modules.repair.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.repair.entity.TroubleCat;
import net.sf.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.*;

/**
 * @Auther: xudong.sun
 * @Date: 2018/12/3 17:05
 * @Description:
 */
@Repository("troubleCatDao")
public class TroubleCatDao implements CrudDao<TroubleCat> {

    private final static Logger logger = LoggerFactory.getLogger(TroubleCatDao.class);

    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        for (String key : queryHash.keySet()) {
            String value = queryHash.get(key);
            switch (key) {
                case "troubleCat":
                    conditionHash.put("s.troubleCatName = ?{paramIndex}", value);
                    continue;
                case "enterpriseName":
                    conditionHash.put("s.enterprise.enterpriseName = ?{paramIndex}", value);
                    continue;
                case "troubleCatId":
                    conditionHash.put("s.id = ?{paramIndex}", StringConverters.null2Long(value));
                    continue;
                case "enterpriseId":
                    conditionHash.put("s.enterprise.id = ?{paramIndex}", StringConverters.null2Long(value));
                    continue;
                case "searchCode":
                    if (StringUtils.isNotBlank(value)) {
                        conditionHash.put("(s.enterprise.enterpriseName like ?{paramIndex} or s.troubleCat like ?{paramIndex} or s.reportMajor.propertyName like ?{paramIndex} and s.organization.organizationName like ?{paramIndex} and s.reportClassify.propertyName like ?{paramIndex})", "%" + value + "%");
                    }
                    continue;
                case "searchName":
                    if (StringUtils.isNotBlank(value)) {
                        conditionHash.put("(s.troubleCatName like ?{paramIndex})", "%" + value + "%");
                    }
                    continue;
                case "searchSerial":
                    if (StringUtils.isNotBlank(value)) {
                        conditionHash.put("(s.serial like ?{paramIndex})", "%" + value + "%");
                    }
                    continue;
            }
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {

        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from TroubleCat s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<TroubleCat> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from TroubleCat s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<TroubleCat> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, TroubleCat.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public TroubleCat findModel(Long userId) {
        return entityManager.find(TroubleCat.class, userId);
    }

    @Override
    public Integer add(TroubleCat troubleCat) {
        troubleCat.setCreateDate(new Date());
        entityManager.persist(troubleCat);
        logger.info("TroubleCatDaoImpl添加troubleCat成功！");
        return 1;
    }

    @Override
    public Integer update(TroubleCat troubleCat) {
        troubleCat.setUpdateDate(new Date());
        entityManager.merge(troubleCat);
        return 1;
    }

    @Override
    public Integer delete(Long troubleCatId) {
        TroubleCat troubleCat = entityManager.find(TroubleCat.class, troubleCatId);
        entityManager.remove(troubleCat);
        return 1;
    }

    @Override
    public Integer deleteLogic(TroubleCat model) {
        model.setDeleteStatus(true);
        return 1;
    }


}
